<%-- 
    Document   : FormularioProceso4
    Created on : 20/10/2011
    Author     : Draggoth
--%>

<%@page import="control.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="control4" class="control.FormularioControl4" scope="page"/>
<jsp:useBean id="form4" class="control.FormularioBean4" scope="session"/>
<jsp:setProperty name="form4" property="*"/>

<%
    //Pasamos el flujo a la capa de control
    String operacion = null;
    String op_status = null;
    String op_msg = null;
    String visita = null;
    boolean exito = false;

    operacion = (String)session.getAttribute("operacion");
    op_status = (String)session.getAttribute("op_status");
    visita = (String)session.getAttribute("visita");

    if( operacion.equals("nuevas") ) {
        if(op_status.equals("inicio")) {

            session.removeAttribute("form4");

            op_status = "guarda";
        }
        else if(op_status.equals("guarda")) {
            exito = control4.guarda(form4);

            if( exito ) {
                op_msg = "exito_guardar";
                op_status = "actualizar";
            }
            else {
                op_msg = "error_guardar";
            }            
        }
        else if( op_status.equals("actualizar") ) {
            exito = control4.actualizar(form4);

            if( exito ) {
                op_msg = "exito_actualizar";
            }
            else {
                op_msg = "error_actualizar";
            }
        }
    }
    else if( operacion.equals("incompletas") ) {
        if(op_status.equals("inicio")) {

            form4.setnIdVisita(Integer.parseInt(visita));
            exito = control4.recupera(form4);

            if( exito ) {
                op_msg = "exito_recupera";
                op_status = "actualizar";
            }
            else {
                exito = control4.guarda(form4);

                if( exito ) {
                    op_msg = "exito_recupera";
                    op_status = "actualizar";
                }
                else {
                    op_msg = "error_recupera";
                    session.removeAttribute("form4");
                    op_status = "recuperar";
                }
            }
        }
        else if( op_status.equals("recuperar") ) {
            form4.setnIdVisita(Integer.parseInt(visita));
            exito = control4.recupera(form4);

            if( exito ) {
                op_msg = "exito_recupera";
                op_status = "actualizar";
            }
            else {
                op_msg = "error_recupera";
                session.removeAttribute("form4");
            }
        }
        else if( op_status.equals("actualizar") ) {
            exito = control4.actualizar(form4);
            
            if( exito ) {
                op_msg = "exito_actualizar";
            }
            else {
                op_msg = "error_actualizar";
            }
        }
    }
    else if( operacion.equals("completas") ) {
        if(op_status.equals("inicio")) {

            form4.setnIdVisita(Integer.parseInt(visita));
            exito = control4.recupera(form4);

            if( exito ) {
                op_msg = "exito_recupera";
                op_status = "observar";
            }
            else {
                op_msg = "error_recupera";
                session.removeAttribute("form4");
                op_status = "recuperar";
            }
        }
        else if( op_status.equals("recuperar") ) {
            form4.setnIdVisita(Integer.parseInt(visita));
            exito = control4.recupera(form4);

            if( exito ) {
                op_msg = "exito_recupera";
                op_status = "observar";
            }
            else {
                op_msg = "error_recupera";
                session.removeAttribute("form4");
            }
        }
    }

    session.setAttribute("operacion", operacion);
    session.setAttribute("op_status", op_status);
    session.setAttribute("op_msg", op_msg);
    response.sendRedirect("formulario4.jsp");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Formulario 2 - Procesando</title>
    </head>
    <body>

    </body>
</html>
